package com.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manage.controller.request.MaterialNumRequest;
import com.manage.controller.response.RepairMaterialLogResponse;
import com.manage.pojo.entity.RepairMaterial;
import com.manage.pojo.entity.RepairMaterialLog;
import com.manage.pojo.entity.RepairTask;
import com.manage.pojo.entity.User;
import com.manage.service.RepairMaterialLogService;
import com.manage.service.RepairMaterialService;
import com.manage.service.RepairTaskService;
import com.manage.service.UserService;
import com.manage.util.ObjectUtil;
import com.manage.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    RepairMaterialService repairMaterialService;

    @Autowired
    RepairTaskService repairTaskService;

    @Autowired
    UserService userService;

    @Autowired
    RepairMaterialLogService repairMaterialLogService;


    @PostMapping("/list")
    public List<RepairMaterial> list() {
        return repairMaterialService.list();
    }

    @PostMapping("/addNum")
    public Result addNum(@RequestBody MaterialNumRequest request) {
        repairMaterialService.addNum(request);
        return Result.success("入库成功");
    }

    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {
        repairMaterialService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody RepairMaterial repairMaterial) {
        repairMaterialService.save(repairMaterial);
        return Result.success("添加成功");
    }

    @GetMapping("/log/list")
    public List<RepairMaterialLogResponse> listLog() {
        List<RepairMaterialLog> list = repairMaterialLogService.list();
        List<Long> userIds = list.stream().map(RepairMaterialLog::getUserId).collect(Collectors.toList());
        List<Long> materialIds = list.stream().map(RepairMaterialLog::getMaterialId).collect(Collectors.toList());
        List<User> userList = userService.list(new QueryWrapper<User>().in("id", userIds));
        List<RepairMaterial> materialList = repairMaterialService.list(new QueryWrapper<RepairMaterial>().in("id", materialIds));
        Map<Long, String> userCollect = userList.stream().collect(Collectors.toMap(User::getId, User::getUsername, (k1, k2) -> k1));
        Map<Long, String> materialCollect = materialList.stream().collect(Collectors.toMap(RepairMaterial::getId, RepairMaterial::getName, (k1, k2) -> k1));
        List<RepairMaterialLogResponse> result = ObjectUtil.cloneList(list, RepairMaterialLogResponse.class);
        result.forEach(item -> {
            item.setMaterialName(materialCollect.get(item.getMaterialId()));
            item.setRepairUserName(userCollect.get(item.getUserId()));
        });
        Collections.reverse(result);
        return result;
    }
}
